package com.csxy.dyq.controller;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.dyq.R;
import com.csxy.dyq.entity.EduTeacher;
import com.csxy.dyq.entity.vo.TeacherQuery;
import com.csxy.dyq.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eduservice/teacher")
@Tag(name = "edu_teacher", description = "讲师管理")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;
    //查询所有讲师
    @Operation(summary = "查询所有讲师")
    @GetMapping("findAll")
    public R getAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }
    //逻辑删除讲师
    @Operation(summary = "逻辑删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R deleteTeacher(@PathVariable("id") String id) {
        boolean result = eduTeacherService.removeById(id);
        if (! result){
            return R.error();
        }
        return R.ok();
    }
    //分页查询
    @Operation(summary = "分页查询")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListTeacher(@PathVariable("page") long page,
                             @PathVariable("limit") long limit) {
        Page<EduTeacher> pagelist =eduTeacherService.pageListTeacher(page,limit);
        return R.ok().data("total", pagelist.getTotal())
                .data("rows", pagelist.getRecords());
    }
    //新增讲师
    @Operation(summary = "新增讲师",description = "新增讲师接口")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
            return R.ok();
        }
        return R.error();
    }
    //根据id查询讲师
    @Operation(summary = "根据id查询讲师")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item", teacher);
    }
    //修改讲师
    @Operation(summary = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }
        return R.error();
    }

    //多条件查询带分页
    @Operation(summary = "多条件查询讲师带分页",description = "多条件查询讲师带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@Parameter(name = "page",description = "当前页码",required = true)
                                  @PathVariable Long  page,
                                  @Parameter( name = "limit",description = "每页记录数",required = true)
                                  @PathVariable Long limit,
                                  @RequestBody(required = false)TeacherQuery teacherQuery) {
        //创建分页page对象
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        //调用方法实现多条件分页查询
        eduTeacherService.pageQuery(pageParam,teacherQuery);
        //获取查询到的数据
        List<EduTeacher> records = pageParam.getRecords();
        //获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

}




